
using System;
using System.Collections;
using QdbUnit;
using QdbUnit.Extensions;
using Qdb.Config;
using Qdb.Events;
using Qdb.Ext;
using Qdb.Tests.Common.Assorted;

namespace Qdb.Tests.Common.Assorted
{
	public class GetUUIDTestCase : AbstractQdbTestCase
	{
		public static void Main(string[] args)
		{
			new GetUUIDTestCase().RunAll();
		}

		/// <exception cref="System.Exception"></exception>
		protected override void Configure(IConfiguration config)
		{
			config.GenerateUUIDs(ConfigScope.Globally);
		}

		/// <exception cref="System.Exception"></exception>
		protected override void Store()
		{
			Store(new GetUUIDTestCase.Item("Item to be deleted"));
		}

		public virtual void TestGetUUIDInCommittedCallbacks()
		{
			QdbUUID itemUUID = GetItemUUID();
			ServerEventRegistry().Committed += new System.EventHandler<Qdb.Events.CommitEventArgs>
				(new _IEventListener4_34(itemUUID).OnEvent);
			DeleteAll(typeof(GetUUIDTestCase.Item));
			Db().Commit();
		}

		private sealed class _IEventListener4_34
		{
			public _IEventListener4_34(QdbUUID itemUUID)
			{
				this.itemUUID = itemUUID;
			}

			public void OnEvent(object sender, Qdb.Events.CommitEventArgs args)
			{
				CommitEventArgs commitEventArgs = (CommitEventArgs)args;
				IEnumerator deletedObjectInfoCollection = commitEventArgs.Deleted.GetEnumerator();
				while (deletedObjectInfoCollection.MoveNext())
				{
					IObjectInfo objectInfo = (IObjectInfo)deletedObjectInfoCollection.Current;
					Assert.AreEqual(itemUUID, objectInfo.GetUUID());
				}
			}

			private readonly QdbUUID itemUUID;
		}

		private QdbUUID GetItemUUID()
		{
			return GetItemInfo().GetUUID();
		}

		private IObjectInfo GetItemInfo()
		{
			return Db().Ext().GetObjectInfo(((GetUUIDTestCase.Item)RetrieveOnlyInstance(typeof(
				GetUUIDTestCase.Item))));
		}

		public virtual void TestGetUUIDInCommittingCallbacks()
		{
			ServerEventRegistry().Committing += new System.EventHandler<Qdb.Events.CommitEventArgs>
				(new _IEventListener4_59().OnEvent);
			DeleteAll(typeof(GetUUIDTestCase.Item));
			Db().Commit();
		}

		private sealed class _IEventListener4_59
		{
			public _IEventListener4_59()
			{
			}

			public void OnEvent(object sender, Qdb.Events.CommitEventArgs args)
			{
				CommitEventArgs commitEventArgs = (CommitEventArgs)args;
				IEnumerator deletedObjectInfoCollection = commitEventArgs.Deleted.GetEnumerator();
				while (deletedObjectInfoCollection.MoveNext())
				{
					IObjectInfo objectInfo = (IObjectInfo)deletedObjectInfoCollection.Current;
					Assert.IsNotNull(objectInfo.GetUUID());
				}
			}
		}

		public class Item
		{
			public string _name;

			public Item(string name)
			{
				_name = name;
			}

			public override string ToString()
			{
				return _name;
			}
		}
	}
}
